package com.big.merchan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.big.cmn.DictFeignClient;
import com.big.merchan.mapper.MerchandiseMapper;
import com.big.merchan.service.MerchandiseInfoService;
import com.big.merchan.service.MerchandiseOffersService;
import com.big.merchan.service.MerchandiseService;
import com.big.merchan.service.MerchandiseSpikeService;
import com.big.model.model.merchandise.MerchandiseDetail;
import com.big.model.model.merchandise.MerchandiseInfo;
import com.big.model.model.merchandise.MerchandiseOffers;
import com.big.model.model.merchandise.MerchandiseSpike;
import com.big.model.vo.merchandise.MerchanParams;
import com.big.model.vo.merchandise.MerchanQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Service
@Slf4j
public class MerchandiseServiceImpl extends ServiceImpl<MerchandiseMapper, MerchandiseDetail> implements MerchandiseService {

    @Autowired
    DictFeignClient dictFeignClient;

    @Autowired
    MerchandiseInfoService merchandiseInfoService;


    @Autowired
    MerchandiseOffersService merchandiseOffersService;

    @Autowired
    MerchandiseSpikeService merchandiseSpikeService;

    //商品列表
    @Override
//    @Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
    public IPage<MerchandiseDetail> MerchanInfos(Page<MerchandiseDetail> pageParam, MerchanParams merchanParams)  {

        //merchanQueryVO 获取条件值
        String  name=merchanParams.getSearch();
        //查询详情
        Integer cId= merchanParams.getCId();

//        对条件值进行非空判断
        QueryWrapper<MerchandiseDetail> wrapper = new QueryWrapper<>();
        /**
         * 返回相应
         */
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(cId)){
            wrapper.eq("cate_id",cId);
        }

        //调用mapper方法
        IPage<MerchandiseDetail> pages = baseMapper.selectPage(pageParam,wrapper);
        pages.getRecords().forEach(item -> {
            this.setGoodType(item);
            try {
                this.setPrice(item);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            this.merchandiseSpikeInfo(item);
        });
        return pages;
    }


    //商品描述
    @Override
    public MerchandiseDetail FindMerchanInfo(String id) {
        QueryWrapper<MerchandiseDetail>wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(id)){
              wrapper.eq("d_id",id);
        }
        MerchandiseDetail info=baseMapper.selectOne(wrapper);
        return info;
    }

    /**
     * 商品描述添加
     * @param merchandiseDetail
     * @return
     */
    @Override
    public Integer SaveDetail(MerchandiseDetail merchandiseDetail) {
        int insert = baseMapper.insert(merchandiseDetail);
        return insert;
    }

    //获取商品list集合,遍历进行商品封装
    private MerchandiseDetail setGoodType(MerchandiseDetail merchandise) {
        //根据value查询
        String StringTagName= dictFeignClient.getName(String.valueOf(merchandise.getTag()));
        String StringCateName= dictFeignClient.getName(String.valueOf(merchandise.getCateId()));
        //优惠方式

        List<MerchandiseOffers> merchandiseOffers = merchandiseOffersService.merchandiseOffersInfos(merchandise.getDId());
        String  StringOffers="";
        if(!(merchandiseOffers.size() <=0)){
        List<String>StringOffersList=new ArrayList<>();
            merchandiseOffers.forEach(item -> {
            System.out.println(item.getOfferCode());
            StringOffersList.add(dictFeignClient.getName(String.valueOf(item.getOfferCode())));
           });
            for (int i = 0; i < StringOffersList.size(); i++) {
                StringOffers+=StringOffersList.get(i);
            }
        }
        merchandise.getParam().put("TagName",StringTagName);
        merchandise.getParam().put("CateName",StringCateName);
//        merchandise.getParam().put("offers",StringOffers);
        return  merchandise;
    }

    //获取商品详情其他信息
    private MerchandiseInfo setPrice(MerchandiseDetail merchandise) throws ParseException {
        //根据id查询 d_id
        MerchandiseInfo infoMerchandise = merchandiseInfoService.findInfoMerchandise(Long.valueOf(merchandise.getDId()));
        //实际售价
        if(infoMerchandise!=null){
        merchandise.getParam().put("selling_price",infoMerchandise.getSellingPrice());
        //原价
        merchandise.getParam().put("original_price",infoMerchandise.getOriginalPrice());
            merchandise.getParam().put("name",infoMerchandise.getName());
        }
        return  infoMerchandise;

    }



    //商品秒杀属性
    public MerchandiseDetail  merchandiseSpikeInfo(MerchandiseDetail merchandiseDetail) {
        MerchandiseSpike merchandiseSpike = merchandiseSpikeService.MerchandiseSpikeInfo(String.valueOf(merchandiseDetail.getDId()));
        if(merchandiseSpike!=null){
            merchandiseDetail.getParam().put("statusDetailName", "秒杀");
        }
        return merchandiseDetail;
    }


}
